<?php
/**
 * DeliveryBill.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-21
 * Time: 19:10
 */

namespace App\Http\Controllers\Api\Business;


use App\Exceptions\RunException;
use App\Http\Controllers\Base;
use App\Tables;

class DeliveryBill extends Base
{
    public function getList()
    {
        $orderList = Tables\Order::where('delivery_status', '!=',Tables\Order::Delivery_Status_Ok)
            ->where('make_status', Tables\Order::Make_Status_Ok)
            ->where('dumped', Tables\Order::No_Dumped)
            ->orderBy('id', 'desc')
            ->get();

        $r = [];
        foreach($orderList as $order) {
            $business = [];
            foreach($order->details as $detail) {
                $consumable = $postPrint = [];
                foreach($detail->consumables()->where('bid', $detail['bid'])->get() as $c) {
                    $consumable[] = $c['c_name'];
                }
                foreach($detail->postPrints()->where('bid', $detail['bid'])->get() as $p) {
                    $postPrint[] = $p['p_name'];
                }
                $business[] = [
                    'id' => $detail['id'],
                    'businessName' => $detail['b_name'],
                    'consumable' => $consumable,
                    'postPrint' => $postPrint,
                    'unit' => $detail['unit'],
                    'content' => $detail['content'],
                    'width' => $detail['width'],
                    'height' => $detail['height'],
                    'number' => $detail['number'],
                    'price' => $detail['price'],
                    'totalPrice' => $detail['total_price'],
                    'area' => Tables\OrderDetail::getArea($detail['count_type'], $detail['width'], $detail['height']),
                    'countType' => $detail['count_type'],
                ];
            }

            $r[] = [
                'id' => $order['id'],
                'no' => $order['id'],
                'customerName' => $order['customer_name'],
                'deliverAddress' => $order['deliver_address'],
                'contactsName' => $order['contacts_name'],
                'contactsPhoneNumber' => $order['contacts_phone_number'],
                'deliveryDate' => $order['delivery_date'],
                'paidPrice' => $order['paid_price'],
                'debt' => Tables\Order::getDebt($order['customer_id']),
                'debtTime' => Tables\Order::getDebtTime($order['customer_id']),
                'deliveryStatus' => $order['delivery_status'],
                'designPrice' => $order['design_price'],
                'postPrintPrice' => $order['post_print_price'],
                'installPrice' => $order['install_price'],
                'taxPrice' => $order['tax_price'],
                'otherPrice' => $order['other_price'],
                'totalPrice' => $order['total_price'],
                'discountPrice' => $order['discount_price'],
                'realPrice' => $order['real_price'],
                'addTime' => date('Y-m-d H:i:s', $order['add_time']),
                'business' => $business
            ];
        }

        return $this->responseSuccess($r);
    }

    public function create()
    {
        $id = (array) $this->request->post('id', []);
        if(empty($id)) {
            return $this->responseFail('无信息');
        }
        $orders = Tables\Order::whereIn('id', $id)->get();
        if($orders->isEmpty()) {
            return $this->responseFail('未找到信息！');
        }
        \DB::beginTransaction();
        try{
            foreach ($orders as $order) {
                if($order['delivery_status'] == Tables\Order::Delivery_Status_No) {
                    $order['delivery_status'] = Tables\Order::Delivery_Status_Ing;
                    if(!(bool) $order->save()) {
                        throw new RunException('操作失败');
                    }
                }
            }
            \DB::commit();
        } catch (RunException $e) {
            \DB::rollBack();
            return $this->responseFail($e->getMessage());
        } catch (\Exception $e) {
            \DB::rollBack();
            return $this->responseFail('数据库错误');
        }
        return $this->responseSuccess();
    }
}